import type { RouteRecordRaw } from "vue-router";

import { createRouter, createWebHashHistory } from "vue-router";
import { homePath } from "@/config/settings";
import { useNProgress } from "@/utils/web/useNProgress";

/***
 * 正式路由
 */
import accountRouter from "./modules/account";
import materialRouter from "./modules/material";
import permissionRouter from "./modules/permission";
import productRouter from "./modules/product";
import productfromRouter from "./modules/productFrom";
import productmodelRouter from "./modules/productionModel";
import technologyRouter from "./modules/technology";

/**
 * 锁定的路由
 */
import demoTestRouter from "./modules/demo";
import lockRouter from "./modules/lock";
import extandRouter from "./modules/extand";

import SecurityLayout from "@/entry/Layout/SecurityLayout.vue";

import MemberLayout from "@/entry/Layout/Member/index.vue";

import UserLayout from "@/entry/Layout/User/index.vue";

export const asyncRoutes: RouteRecordRaw[] = [
	accountRouter,
	materialRouter,
	permissionRouter,
	productRouter,
	productfromRouter,
	productmodelRouter,
	technologyRouter,
];

export const lockRoutes: RouteRecordRaw[] = lockRouter.concat(demoTestRouter);

const allRoutes: RouteRecordRaw[] = lockRoutes.concat(asyncRoutes);

const { start, done } = useNProgress();

export const routes: RouteRecordRaw[] = [
	{
		path: "/",
		component: SecurityLayout,
		children: [
			{
				path: "/",
				redirect: homePath,
				component: MemberLayout,
				children: allRoutes,
			},
		],
	},

	{
		path: "/",
		component: UserLayout,
		children: extandRouter,
	},

	{
		path: "/:pathMatch(.*)*",
		component: () => import("@/entry/Error/Page404.vue"),
	},
];

const router = createRouter({
	history: createWebHashHistory(),
	routes,
	scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * @description 路由前置，拦截
 */
router.beforeEach((to, from, next) => {
	// start progress bar
	start();

	// 在跳转之前，清除所有ajax请求
	//requestCanceler.removeAllPending();
	// console.log(requestCanceler, to, from, next);
	// 跳转到对应路由
	next();
});

/**
 * @description 路由后置，跳转结束
 */
router.afterEach(() => {
	// finish progress bar
	done();
});

export default router;
